package pub.tbc.atps.process;

import com.google.common.collect.Maps;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author tbc on 2016/12/3 12:10.
 */
public class HandlerContext {
    Map<String, Object> attributes = new ConcurrentHashMap<>();

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes.clear();
        this.attributes.putAll(attributes);
    }

    public void setAttribute(String key, Object value) {
        attributes.put(key, value);
    }

    @SuppressWarnings("uncheked")
    public <T> T getAttribute(String key) {
        return (T) attributes.get(key);
    }

    public Set<String> getAttributeNames() {
        return attributes.keySet();
    }

    public Map<String, Object> getAttributes() {
        return attributes;
    }

    public Map<String, Object> getNameFilterAttributes(String name) {
        Map<String, Object> map = Maps.newHashMap();
        attributes.forEach((k, v) -> {
            if (k.equals(name)) {
                map.put(k, v);
            }
        });
        return map;
    }
}
